package com.example.express;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.example.express.base.BaseActivity;
import com.example.express.entity.User;
import com.example.express.utlis.MD5Utils;
import com.example.express.utlis.ToastUtil;

import org.litepal.LitePal;

public class MainActivity extends BaseActivity {

    public static void start(Context context){
        context.startActivity(new Intent(context,MainActivity.class));
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initData() {

    }
 
    @Override
    protected void initView() {
        CheckBox checkBox = findViewById(R.id.m_is_visible_cb);
        TextView pass = findViewById(R.id.m_password_et);
        TextView agreement = findViewById(R.id.m_agreement);
        TextView protocol = findViewById(R.id.m_protocol);
        TextView forgetPassword = findViewById(R.id.m_forget_password_tv);
        agreement.setOnClickListener(v -> AgreementActivity.start(this,"用户协议"));
        protocol.setOnClickListener(v -> AgreementActivity.start(this,"隐私政策"));
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    pass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    pass.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });
        findViewById(R.id.m_commit_tv).setOnClickListener(v ->
                login()
                );
        forgetPassword.setOnClickListener(v -> RegisterActivity.start(this,true));
        findViewById(R.id.m_register_tv).setOnClickListener(v -> RegisterActivity.start(this,false));
        findViewById(R.id.m_forget_password_tv);
    }
    private void login() {
        EditText etPhone = findViewById(R.id.m_phone_et);
        EditText etPassword = findViewById(R.id.m_password_et);
        String phone = etPhone.getText().toString().trim();
        String password = etPassword.getText().toString().trim();
        password = MD5Utils.digest(password);
        if (TextUtils.isEmpty(phone)) {
            ToastUtil.show("请输入账号");
            return;
        }

        if (TextUtils.isEmpty(password)) {
            ToastUtil.show("请输入密码");
            return;
        }

        User user = LitePal.where("phone = ? and password = ?", phone,password)
                .findFirst(User.class);

        if(user==null){
            ToastUtil.show("账号密码错误");
            return;
        }

        QueryActivity.start(this);
        finish();

    }
}